<?php
/*
+------------------------------------------------------------------------------+
|   Rooster - a plugin by Francis Cheung
|
|	Plugin Support Site: e107.webstartinternet.com
|
|	For the e107 website system visit http://e107.org
|
|	Released under the terms and conditions of the
|	GNU General Public License (http://gnu.org).
+------------------------------------------------------------------------------+
*/
require_once('../../class2.php');
require_once('rooster_class.php');

if (!defined('e107_INIT')) { exit(); }
require_once(HEADERF);
include_lan(e_PLUGIN.'rooster/languages/'.e_LANGUAGE.'.php');
//require_once('eg_class.php');
//require_once(e_HANDLER.'comment_class.php');
//$cobj = new comment;

// +++++++++++++++E107 UPDATABLE VARIABLES BELOW:
// Stop caching for all browsers
session_cache_limiter('nocache');
// Start a session to catch the image list
session_start();

// Get the current selected date
if (isset($_GET['selected_date']))
{
	$str_curr_selected_date=$_GET['selected_date'];
}
else
{
	$str_curr_selected_date=date('Y-m-d',mktime());
}

if (isset($_GET['church_type']))
{
	$str_selected_church_type=$_GET['church_type'];
}
else
{
	$str_selected_church_type=1;
}

// Save data to rooster_tbl if there's data in POST
if (isset($_GET['church_type']) && isset($_GET['job_id']) && isset($_GET['job_date']) && isset($_GET['user_id']))
{
	if ($_GET['user_id'] == 0)
	{
		$sql->db_Delete("rooster_tbl","church_id=".$_GET['church_type']." AND job_id=".$_GET['job_id']." AND job_date='".$_GET['job_date']."'");
	}
	else
	{
		if ($sql->db_Select_gen("SELECT count(*) as rec_count FROM ozs_rooster_tbl WHERE church_id=".$_GET['church_type']." AND job_id=".$_GET['job_id']." AND job_date='".$_GET['job_date']."'", true))
		{
			list($str_rec_count)=$sql->db_Fetch();
			
			if ($str_rec_count > 0)
			{
				if ($_GET['user_id'] == 99)
				{
					$sql->db_Update("rooster_tbl","user_id=".$_GET['user_id'].", guest_name='".$_GET['guest_name']."' WHERE church_id=".$_GET['church_type']." AND job_id=".$_GET['job_id']." AND job_date='".$_GET['job_date']."'", true);
				}
				else
				{
					$sql->db_Update("rooster_tbl","user_id=".$_GET['user_id'],"  WHERE church_id=".$_GET['church_type']."AND job_id=".$_GET['job_id']." AND job_date='".$_GET['job_date']);
				}
			}
			else 
			{
				if ($_GET['user_id'] == 99)
				{
					$sql->db_Select_gen("INSERT INTO ozs_rooster_tbl (church_id, job_id, user_id, job_date, guest_name) VALUES (".$_GET['church_type'].",".$_GET['job_id'].",".$_GET['user_id'].",'".$_GET['job_date']."','".$_GET['guest_name']."')", true);
				}
				else
				{
					$sql->db_Select_gen("INSERT INTO ozs_rooster_tbl (church_id, job_id, user_id, job_date) VALUES (".$_GET['church_type'].",".$_GET['job_id'].",".$_GET['user_id'].",'".$_GET['job_date']."')", true);
				}
			}
		}
	}	
}

if ($sql->db_Select("church_tbl",'*',''))
{
	while (list($str_church_id, $str_church_type)=$sql->db_Fetch())
	{
		$arr_church[$str_church_id]=$str_church_type;
	}
}

if ($sql->db_Select("church_user_tbl"))
{
	while (list($user_id,$church_id,$user_name)=$sql->db_Fetch())
	{
		$arr_users[$user_id]=$user_name;
	}
}

if ($sql->db_Select("activity_tbl","id, name","church_id=".$str_selected_church_type,"default"))
{
	while (list($activity_id,$activity_name)=$sql->db_Fetch())
	{
		$arr_activity[$activity_id]=$activity_name;
	}
}
$arr_Sunday=getDateForSpecificDayBetweenDates($str_curr_selected_date, 0, 4);

// Render the value of $text in a table.
$title = RS_CORE_00;
$arr_selected_date=preg_split("/\-/",$str_curr_selected_date);
$text = "<table border=1><tr>";
$text .="<td><a href='rooster.php?selected_date=".date('Y-m-d',mktime(0,0,0,$arr_selected_date[1],$arr_selected_date[2],$arr_selected_date[0])-(28*24*3600))."'>Prev 4 weeks</a></td>";
$text .="<td><a href='rooster.php?selected_date=".date('Y-m-d',mktime(0,0,0,$arr_selected_date[1],$arr_selected_date[2],$arr_selected_date[0])+(28*24*3600))."'>Next 4 weeks</a></td>";
$text .="</tr></table>";

$text .= "<input type='button' value='印刷本' onClick='window.open(\"dutypage.php?selected_date=".$str_curr_selected_date."\")'></input>";
$text .= "<input type='button' value='傳譯影音' onClick='window.open(\"avpage.php?selected_date=".$str_curr_selected_date."\")'></input>";

$text .= "<table border=1><tr>";

if ((time() - mktime(0,0,0,$arr_selected_date[1],$arr_selected_date[2],$arr_selected_date[0]))< 4*7*24*3600)
{
	$bool_editable=true;
}
else
{
	$bool_editable=false;
}

foreach ($arr_church as $key=>$value)
{
	if ($str_selected_church_type==$key)
	{
		$text .= "<td>".$value."</td>";
	}
	else
	{
		$text .= "<td><a href=''>".$value."</a></td>";
	}
}

$text .= "</tr></table>";

foreach ($arr_activity as $key=>$value)
{
	$text .= "<table border=1>";
	$text .= "<tr><td>".$value."</td></tr>";
	
	if ($sql->db_Select("job_tbl","*","activity_id=".$key,"default"))
	{
		while (list($job_id,$activity_id,$job_name)=$sql->db_Fetch())
		{
			$arr_job[$job_id]=$job_name;
		}
	}
	$text .="<tr><td>日期</td>";

	foreach ($arr_job as $key=>$value)
	{
		$text .= "<td>".$value."</td>";
	}
	$text .="</tr>";

	$int_user_count=count($arr_job);
	$arr_job_ids=array_keys($arr_job);
	foreach ($arr_Sunday as $key=>$value)
	{
		$text .= "<tr><td>".$value."</td>";

		for ($i=0; $i<$int_user_count; $i++)
		{
			if ($sql->db_Select("rooster_tbl","user_id, guest_name","church_id=".$str_selected_church_type." and job_date='".$value."' and job_id=".$arr_job_ids[$i], "default"))
			{
				list($user_id, $str_guest_name)=$sql->db_Fetch();
				if ($user_id != '99')
				{
					if ($sql->db_Select("church_user_tbl","name","id=".$user_id))
					{
						list($str_user_name)=$sql->db_Fetch();
					}
				}
				else
				{
					$str_user_name=$str_guest_name;
				}
			}
			else
			{
					$str_user_name="未填滿";
			}
			if ($bool_editable)
			{
				$text .= "<td><a href='jobpage.php?church_id=".$str_selected_church_type."&job_id=".$arr_job_ids[$i]."&job_date=\"".$value."\"&user_id=".$user_id."'>".$str_user_name."</a></td>";
			}
			else
			{
				$text .= "<td>".$str_user_name."</td>";
			}
		}
		
		$text .= "</tr>";
	}

	$text .= "</table>";
	unset($arr_job);
}
$ns -> tablerender($title, $text);

// === End of BODY ===
// use FOOTERF for USER PAGES and e_ADMIN.'footer.php' for admin pages
require_once(FOOTERF);
?>
